#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__autho__ = "Tony.Don"
__lastupdatetime__ = "2017/09/20"
import threading
import time,datetime
import queue
import traceback
from threading import current_thread
from core.Spider import myhttp
from core.parser import parserHtml
from models import domain


class feed(threading.Thread):
    __mt__=myhttp()
    __rules={}
    __running=threading.Event()
    __lock=threading.RLock()
    #_taskmrg=None
    def __init__(self,taskmrg):
        self._taskmrg=taskmrg
        threading.Thread.__init__(self,name=str(self._taskmrg.__poollist__.getCount()))
        self.setName(str(self._taskmrg.__poollist__.getCount()))
        self.__running.set()
        print("{0} Init thread pool success!\r\n".format(str(self.name)))
        #self.start()

    def stop(self):
        self.__running.clear()

    def run(self):
        try:
            while self.__running.isSet():
                args = self._taskmrg.getTask()
                if args:
                    print("{0}-Get remote web({1}) content by http\r\n".format(str(self.name),args[0]))
                    content,status_code = self.__mt__.getcontent(args[0])
                    if content and status_code==200:
                        if args[1] not in self.__rules:
                            handle=__import__("rule."+args[1],fromlist=(True))
                            if hasattr(handle,args[1]):
                                func=getattr(handle,args[1])
                                self.__rules[args[1]]=func    
                        if args[1] in self.__rules:
                            print("{0}-Start analysis html by {1},please wait!\r\n".format(str(self.name),args[1]))
                            self.__lock.acquire()
                            self.__rules[args[1]](args[0]).getRule(content)
                            self.__lock.release()
                    else:
                        domain.domain().remove({"name":args[0]})
                        print("%s-%s Get content error,HTTP Status Code: %d" % (self.name,args[0],status_code))
                else:
                    print("{0}-Don't has some task to run!\r\n".format(str(self.name)))
                print("{0}-Have an process has complete!\r\n".format(self.name))
                self._taskmrg.complete(self)       
        except Exception as e:
            print("{0}-Has an error msg:\r\n{1}".format(self.name,e))
            print('str(Exception):\t', str(Exception))
            print('str(e):\t\t', str(e))
            print('repr(e):\t', repr(e))
           # print('e.message:\t', e.message)
            print('traceback.print_exc():', traceback.print_exc())
            print('traceback.format_exc():\n%s' % traceback.format_exc())
            print("{0}-Have an process has complete!\r\n".format(self.name))
            self._taskmrg.complete(self)
            
            